package com.night.xjh.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: RabbitMQConfig
 * @Description: //TODO
 * @author: xjh
 * @date: 2022/10/28-15:52
 * @version: v1.0
 */
@Configuration
public class RabbitMQConfig {
    //声明交换机
    @Bean
    public TopicExchange exchangeXjhTopic(){
        return new TopicExchange("exchangeXjhTopic");
    }

    //声明队列
    @Bean
    public Queue queueXjh(){
        return new Queue("queueXjh");
    }
    @Bean
    public Binding bindingXjh(Queue queueXjh, TopicExchange exchangeXjhTopic){
        return BindingBuilder.bind(queueXjh).to(exchangeXjhTopic).with("#.xjh.#");
    }
    //死信队列，实现延迟队列
    //一组交换机和队列，队列没有消费者，就是让它变成死信
    @Bean
    public Queue queueXjhDelay(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        // @Nullable Map<String, Object> arguments  队列属性
        Map<String, Object> arguments=new HashMap<>();
        //arguments.put("x-message-ttl",10000);
        arguments.put("x-dead-letter-exchange","exchangeXjhDLX");
        arguments.put("x-dead-letter-routing-key","xjhDLX");
        return new Queue("queueXjhDelay",true,false,false,arguments);
    }
    //定义死信交换机和死信队列
    @Bean
    public TopicExchange exchangeXjhDLX(){
        return new TopicExchange("exchangeXjhDLX");
    }

    //声明队列
    @Bean
    public Queue queueXjhDLX(){
        return new Queue("queueXjhDLX");
    }
    //队列绑定交换机路由
    @Bean
    public Binding bindingOrderDLX(Queue queueXjhDLX, TopicExchange exchangeXjhDLX){
        return BindingBuilder.bind(queueXjhDLX).to(exchangeXjhDLX).with("#.xjhDLX.#");
    }
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
